package tech.eyao.hs60x.serviceImpl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.PushPayload;
import tech.eyao.hs60x.common.ReturnCode;
import tech.eyao.hs60x.dao.AccountRelationDao;
import tech.eyao.hs60x.dto.BaseResult;
import tech.eyao.hs60x.enums.EventType;
import tech.eyao.hs60x.enums.ExceptionCodeEnum;
import tech.eyao.hs60x.manager.IdGenService;
import tech.eyao.hs60x.model.domain.AccountRelationDO;
import tech.eyao.hs60x.service.CommandService;
import tech.eyao.hs60x.utils.JPushUtils;
import tech.eyao.hs60x.vo.Command;

/**
 * @author hejian
 * @date 2018年1月26日 上午11:51:25
 */
@Service
public class CommandServiceImpl implements CommandService {

	private static final Logger	logger	= LoggerFactory.getLogger(CommandServiceImpl.class);

	@Autowired
	private AccountRelationDao	accountRelationDao;
	
	@Autowired
    private IdGenService    idGenService    = null;

    @Override
	public BaseResult sendCommand(long accountId, int code, long watchId, String data) {
		logger.info("[CommandService sendCommand] accountId={},code={},watchId={},json={}", accountId, code, watchId,
				data);
		BaseResult baseResult = new BaseResult();
		if (accountId <= 0 || code <= 0 || watchId <= 0) {
		    return BaseResult.isFailure(ExceptionCodeEnum.ERROR_PARAMERROR.getCode(), ExceptionCodeEnum.ERROR_PARAMERROR.getMessage());

		}
		// check to from 绑定关系是否存在
		AccountRelationDO relation = accountRelationDao.getAccountRelation(accountId, watchId);
		if (null == relation) {
		    return BaseResult.isFailure(ExceptionCodeEnum.RECORD_NOT_EXISTS.getCode(), ExceptionCodeEnum.RECORD_NOT_EXISTS.getMessage());
		}
		
		try {
			// 是否为手表管理员
			Command command = new Command(code, data, System.currentTimeMillis(), idGenService.nextId(), watchId);
			PushPayload payload = JPushUtils.sendMeassage(JSON.toJSONString(command), String.valueOf(watchId));
			PushResult result = JPushUtils.getWatchInstance().sendPush(payload);
			logger.info("[CommandService sendCommand] result = {}", JSON.toJSON(result));
		} catch (Exception e) {
			logger.error("[CommandService sendCommand]", e);
		}
		// db
		baseResult.code = ReturnCode.C_SUCCESS_CODE;
		baseResult.message = ReturnCode.SUCCESS_CODE.getDesc();
		return baseResult;
	}

	@Override
	public BaseResult notification(long accountId, int code, long watchId, String data) {
		logger.info("[CommandService notification] accountId={},code={},watchId={},json={}", accountId, code, watchId,
				data);
		BaseResult baseResult = new BaseResult();
		if (accountId <= 0 || code <= 0 || watchId <= 0) {
		    return BaseResult.isFailure(ExceptionCodeEnum.ERROR_PARAMERROR.getCode(), ExceptionCodeEnum.ERROR_PARAMERROR.getMessage());

		}
		// check to from 绑定关系是否存在
		AccountRelationDO relation = accountRelationDao.getAccountRelation(accountId, watchId);
		if (null == relation) {
		    return BaseResult.isFailure(ExceptionCodeEnum.RECORD_NOT_EXISTS.getCode(), ExceptionCodeEnum.RECORD_NOT_EXISTS.getMessage());
		}
		
		// 是否为手表管理员
		Command command = new Command(code,data);
		PushPayload payload = JPushUtils.buildPushObject_all_alias_alert(String.valueOf(watchId), JSON.toJSONString(command));
		try {
			PushResult result = JPushUtils.getWatchInstance().sendPush(payload);
			logger.info("[CommandService notification] result = {}", JSON.toJSON(result));
		} catch (Exception e) {
			logger.error("[CommandService notification]", e);
		}
		// db
		baseResult.code = ReturnCode.C_SUCCESS_CODE;
		baseResult.message = ReturnCode.SUCCESS_CODE.getDesc();
		return baseResult;
	}
}
